package databases

import (
	"errors"

	"github.com/go-xorm/xorm"
	"gitlab.gaodun.com/golib/gderror"
	"permission/config"
	"permission/vigo_loger"
)

type UserRolesFactory struct {
	Table        Tpo_Sys_User_Roles
	engine       *xorm.Engine
	RowsSlicePtr []Tpo_Sys_User_Roles
}

// 返回 factory
func NewUserRolesFactory() *UserRolesFactory {
	funcFac := UserRolesFactory{}
	funcFac.engine, _ = config.InitEng()
	return &funcFac
}

func (urf *UserRolesFactory) FindInfoByUserId(userId int64) error {
	if userId == 0 {
		return errors.New("user id is null")
	}
	err := urf.engine.Where(" UserId = ? ", userId).Find(&(urf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return err
}

func (urf *UserRolesFactory) FindInfoByUserIds(userIds ...int64) error {
	if len(userIds) == 0 {
		return errors.New("user id is null")
	}
	err := urf.engine.In("UserId", userIds).Find(&(urf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return err
}

//
func (urf *UserRolesFactory) FindInfoByRoleId(roleId int64) error {
	if roleId == 0 {
		return errors.New("role id is null")
	}
	err := urf.engine.Where(" RoleId = ? ", roleId).Find(&(urf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return err
}

func (urf *UserRolesFactory) FindAllInfo() error {
	err := urf.engine.Where(" 1 = ? ", 1).Find(&(urf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return err
}
